//
//  TabBarViewController.m
//  Weibo
//
//  Created by Anasue on 9/9/15.
//  Copyright (c) 2015 Anasue. All rights reserved.
//

#import "TabBarViewController.h"
#import "HomeViewController.h"
#import "DiscoverViewController.h"
#import "MessageViewController.h"
#import "ProfileViewController.h"
#import "NavigationController.h"
#import "UIImage+Extension.h"
#import "TabBar.h"
#import "ComposeViewController.h"
#import "HttpTool.h"
#import "AccountTool.h"
#import "Account.h"
#import "HomeViewController.h"
#import "UserTool.h"
#import "MessageViewController.h"
#import "ProfileViewController.h"

@interface TabBarViewController ()<TabBarDelegate,UITabBarControllerDelegate>
@property (nonatomic, weak) HomeViewController *home;
@property (nonatomic, weak) MessageViewController *message;
@property (nonatomic, weak) ProfileViewController *profile;
@property (nonatomic, weak) UIViewController *lastSelectedVc;

@end

@implementation TabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置所有自控制器
    [self setupAllChildVcs];
    //设置自定义TabBar
    [self setupCustomTabBar];    
    self.delegate = self;
    
    //利用定时器设置消息未读数
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(getUnreadCount) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

- (void)setupCustomTabBar
{
    //自定义TabBar
    TabBar * tabBar = [[TabBar alloc]init];
    tabBar.tabBardelegate = self;
    [self setValue:tabBar forKeyPath:@"tabBar"];

}

- (void)setupAllChildVcs
{
    HomeViewController *home = [[HomeViewController alloc]init];
    [self addOneChildVc:home title:@"首页" imageName:@"tabbar_home" selectedImageName:@"tabbar_home_selected"];
    self.home = home;
    self.lastSelectedVc = home;
    
    MessageViewController *message = [[MessageViewController alloc]init];
    [self addOneChildVc:message title:@"消息" imageName:@"tabbar_message_center" selectedImageName:@"tabbar_message_center_selected"];
    
    DiscoverViewController *discover = [[DiscoverViewController alloc]init];
    [self addOneChildVc:discover title:@"发现" imageName:@"tabbar_discover" selectedImageName:@"tabbar_discover_selected"];
    
    ProfileViewController *profile = [[ProfileViewController alloc]init];
    [self addOneChildVc:profile title:@"我" imageName:@"tabbar_profile" selectedImageName:@"tabbar_profile_selected"];
}

/**
 *  监听未读数
 */
- (void)getUnreadCount
{
    UnreadCountParam *param = [UnreadCountParam param];
    param.access_token = [AccountTool account].access_token;
    param.uid = [AccountTool account].uid;
    
    [UserTool unreadCountWithParams:param success:^(UnreadCountResult *result) {
        //首页未读数
        if (0 == result.status) {
            self.home.tabBarItem.badgeValue = nil;
        }else
        {
            self.home.tabBarItem.badgeValue = [NSString stringWithFormat:@"%D",result.status];
        }
        //消息未读数
        if (0 == result.messageCount) {
            self.message.tabBarItem.badgeValue = nil;
        }else
        {
            self.message.tabBarItem.badgeValue = [NSString stringWithFormat:@"%D",result.messageCount];
        }
        //我的未读数
        if (0 == result.follower) {
            self.profile.tabBarItem.badgeValue = nil;
        }else
        {
            self.profile.tabBarItem.badgeValue = [NSString stringWithFormat:@"%D",result.follower];
        }
        [UIApplication sharedApplication].applicationIconBadgeNumber = result.totalCount;
    } failure:^(NSError *result) {
    }];
}


#pragma mark - UITabBarDelegate

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UINavigationController *)viewController
{
    UIViewController *vc = [viewController.viewControllers firstObject];
    if ([vc isKindOfClass:[HomeViewController class]]) {
        if (self.lastSelectedVc == vc) {
            [self.home refresh:YES];
        }
    }else{
            [self.home refresh:NO];
    }
    self.lastSelectedVc = vc;
    
}


- (void)addOneChildVc:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
//    childVc.view.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0];
    childVc.tabBarItem.image = [UIImage imageWithName:imageName];
    childVc.title = title;
    
    //设置字体及颜色
    NSMutableDictionary *textAttr = [NSMutableDictionary dictionary];
    textAttr[NSForegroundColorAttributeName] = [UIColor blackColor];
    textAttr[NSFontAttributeName] = [UIFont systemFontOfSize:11];
    [childVc.tabBarItem setTitleTextAttributes:textAttr forState:UIControlStateNormal];
    NSMutableDictionary *selectedTextAttr = [NSMutableDictionary dictionary];
    selectedTextAttr[NSForegroundColorAttributeName] = [UIColor orangeColor];
    [childVc.tabBarItem setTitleTextAttributes:selectedTextAttr forState:UIControlStateSelected];

    
    
    UIImage *selectedImage = [UIImage imageWithName:selectedImageName];
    if (iOS7) {
        selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    childVc.tabBarItem.selectedImage =selectedImage;
    
    //添加导航控制器
    NavigationController *nvc = [[NavigationController alloc]initWithRootViewController:childVc];
//    nvc.title = title;
    [self addChildViewController:nvc];
}

#pragma  mark -TabBarDelegate
- (void)tabBarDidClickedPlusButton:(TabBar *)tabBar
{
    ComposeViewController *cvc = [[ComposeViewController alloc]init];
    NavigationController *nav = [[NavigationController alloc]initWithRootViewController:cvc];
    [self presentViewController:nav animated:YES completion:nil];
}


@end
